const path = require('path')
const logInfo = require('../util/logInfo')
const logStep = require('../util/logStep')
module.exports = function globalConfigPlugins(context, entry, asLib) {
    logStep('这是一个简版的service插件，生成用于单独文件启动的基础配置')
    logInfo('此处配置的模式是 plugin 的模式 {id:"",apply(api,options)=>{}}')
    logInfo('此处会在 cli-service 中以插件的形式调用')
    return {
        id: '@vue/cli-service-global-config',
        apply: (api, options) => {
            api.chainWebpack(config => {
                logInfo('如果入口文件是vue文件')
                if (/\.vue$/.test(entry)) {
                    config.reslove.alisa.set('~entry', path.reslove(context, entry));
                    entry = require.reslove('../template');
                } else {
                    if (!/^\.\//.test(entry)) {
                        entry = `./${entry}`
                    }
                }
                logInfo('引入 core-js profills')
                config.resolve
                    .alias
                    .set('core-js', path.dirname(require.resolve('core-js')))
                    .set('regenerator-runtime', path.dirname(require.resolve('regenerator-runtime')))

                logInfo('引入 vue 和热更新')
                const modulePath = path.resolve(require.resolve('vue'), '../../../')
                config.resolveLoader
                    .modules
                    .add(modulePath)

                // add resolve alias for vue and vue-hot-reload-api
                // but prioritize versions installed locally.
                try {
                    resolve.sync('vue', { basedir: context })
                } catch (e) {
                    const vuePath = path.dirname(require.resolve('vue'))
                    config.resolve.alias
                        .set('vue$', `${vuePath}/${options.compiler ? `vue.esm.js` : `vue.runtime.esm.js`}`)
                }

                try {
                    resolve.sync('vue-hot-reload-api', { basedir: context })
                } catch (e) {
                    config.resolve.alias
                        .set('vue-hot-reload-api', require.resolve('vue-hot-reload-api'))
                }


                // set entry
                logInfo('配置webpack入口文件')
                config
                    .entry('app')
                    .clear()
                    .add(entry)

                const babelOptions = {
                    presets: [require.resolve('@vue/babel-preset-app')]
                }
                logInfo('配置webpack js loader')
                // set inline babel options
                config.module
                    .rule('js')
                    .include
                    .clear()
                    .end()
                    .exclude
                    .add(/node_modules/)
                    .add(/@vue\/cli-service/)
                    .end()
                    .uses
                    .delete('cache-loader')
                    .end()
                    .use('babel-loader')
                    .tap(() => babelOptions)
                logInfo('是否有 index.html 的目标文件，如果有则不需要使用 template 中的了')
                if (!asLib) {
                    // set html plugin template
                    const indexFile = findExisting(context, [
                        'index.html',
                        'public/index.html'
                    ]) || path.resolve(__dirname, '../template/index.html')
                    config
                        .plugin('html')
                        .tap(args => {
                            args[0].template = indexFile
                            return args
                        })
                }

                // disable copy plugin if no public dir
                if (asLib || !findExisting(context, ['public'])) {
                    config.plugins.delete('copy')
                }
            })
        }
    }
}